package com.xcw.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class AddPhotoWaterMark {


    private static final Logger logger = LoggerFactory.getLogger("pay");

    public static BufferedImage mark(String srcImgPath, String address, String time, String name) throws IOException {
        try {
            // 读取原图片信息
            Image srcImg = ImageIO.read(getImageStream(srcImgPath));
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);

            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            Font font = new Font("宋体", Font.PLAIN, (int) (srcImgWidth * 0.03));
            g.setColor(Color.WHITE); // 根据图片的背景设置水印颜色
            g.setFont(font);

            String str = address;

            if (getWatermarkLength(str, g) > (srcImgWidth - (srcImgWidth * 0.1))) {
                Long v = (long) ((srcImgWidth - srcImgWidth * 0.1) / (int) (srcImgWidth * 0.03));

                g.drawString(name, (float) (srcImgWidth * 0.05), (float) ((srcImgHeight * 0.96 - srcImgWidth * 0.03 * 4)));
                g.drawString(time, (float) (srcImgWidth * 0.05), (float) (srcImgHeight * 0.97 - srcImgWidth * 0.03 * 3));
                g.drawString(str.substring(0, Math.toIntExact(v)), (float) (srcImgWidth * 0.05), (float) (srcImgHeight * 0.98 - srcImgWidth * 0.03 * 2));
                g.drawString(str.substring(Math.toIntExact(v)), (float) (srcImgWidth * 0.05), (float) (srcImgHeight * 0.98 - srcImgWidth * 0.03));
            } else {

                g.drawString(name, (float) (srcImgWidth * 0.05), (float) ((srcImgHeight * 0.96 - srcImgWidth * 0.03 * 3)));
                g.drawString(time, (float) (srcImgWidth * 0.05), (float) (srcImgHeight * 0.97 - srcImgWidth * 0.03 * 2));
                g.drawString(str, (float) (srcImgWidth * 0.05), (float) (srcImgHeight * 0.98 - srcImgWidth * 0.03));
            }
            g.dispose();
            return bufImg;
        } catch (IOException e) {
                    System.out.println("加水印异常，图片路径为：" + srcImgPath);
                    e.printStackTrace();
                }
        return null;
    }

    /**
     * 获取水印文字总长度
     *
     * @param waterMarkContent 水印的文字
     * @param g
     * @return 水印文字总长度
     */
    public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    /**
     * 获取网络图片流
     *
     * @param url
     * @return
     */
    private static InputStream getImageStream(String url) throws IOException {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                return inputStream;
            }
        } catch (IOException e) {
            System.out.println("获取网络图片出现异常，图片路径为：" + url);
            e.printStackTrace();
        }
        return null;
    }
}
